define(
   [
      'dojo/_base/declare',
      'dojo/_base/lang',
      'dojo/on',
      'dijit/Dialog'
   ],
   function(declare, lang, on, Dialog)
   {
      return declare([Dialog],
      {
         id:"OslcDialogDelegatedResourceBaseDlg",
         title:'Oslc Delegated Resource',
         width:'auto',
         height:'auto',
         constructor:function(args) {
            // Call Super
            dojo.safeMixin(this, args);
         },
         postscript:function(args) {
            this.inherited(arguments);
      
            var width = this.width;
            if (!isNaN(this.width - 0))
               width = this.width + 50;
            else if (width.indexOf('px') != -1) {
               width = this.width.substring(0, this.width.length - 2);
               width = parseInt(width,10) + 50;
            }
      
            var height = this.height;
            if (!isNaN(this.height - 0))
               height = this.height + 50;
            else if (this.height.indexOf('px') != -1) {
               height = this.height.substring(0, this.height.length - 2);
               height = parseInt(height,10) + 50;
            }
      
            this.setAttribute('style', 'width: ' + width + '; height: ' + height + '');
      
            var signal = on(window, 'message', lang.hitch(this, function(e)
            {
               var HEADER = "oslc-response:";
               if (e.source != dojo.byId("OslcDialogDelegatedResourceBaseDlgIframe").contentWindow || e.data.indexOf(HEADER) !== 0)
                  return;

               signal.remove();
               var data = (e.data.substr(HEADER.length));
               if(data === '')
               {
                  this.gotResult(null);
                  return;
               }
               var jsonData = dojo.fromJson(data);

               var results = jsonData['oslc:results'];
               this.preResultsEvent(results);
               if (!results || results.length === 0)
               {
                  this.gotResult(null);
                  return;
               }

               for(var i = 0; i < results.length; ++i)
                  this.gotResult(results[i]);

               this.postResultsEvent(results);
            }));

            dojo.connect(this, "onHide", this, function(e) 
            {
               dojo.hitch(this, setTimeout(function() 
               {
                  signal.remove();
                  dijit.byId("OslcDialogDelegatedResourceBaseDlg").destroyRecursive();
               }, 50));
            });
      
            this.show();
         },
         displayUrl: function()
         {
            this.attr('content', '<iframe src="' + this.dialogUrl + '#oslc-core-postMessage-1.0" width="' + this.width + '" height="' + this.height + '" border="0" scrolling="auto" id="OslcDialogDelegatedResourceBaseDlgIframe"/>');
         },
         gotResult:function(result, results) {
         },
         preResultsEvent:function(results) {
         },
         postResultsEvent:function(results) {
         }
      });
   }
);
